<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的待办</title>
</head>
<body>
<div style="padding:10px;background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">我的工作台/待办管理</div>
                <div class="layui-card-body">
                    <!--数据表格-->
                    <table class="layui-hide" id="emp_table" lay-filter="emp_table"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--表格中操作：编辑、删除-->
<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-xs" lay-event="emp_edit">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="emp_del">处理</a>
</script>
<script type="text/html" id="operation1">
    <a class="layui-btn layui-btn-xs" lay-event="update_grade">确认级别</a>
</script>
<script>
    layui.use('table', function(){
        var table = layui.table;
        var user=JSON.parse(sessionStorage.getItem("users"));
        if (user[0].role_id == 3){
            console.log("巡检");
            table.render({
                elem: '#emp_table'
                ,url:'/needDealt.do'
                ,toolbar: '#toolbarDemo'
                ,title: '待办列表'
                ,where:{user_account:user[0].user_account,role_id:user[0].role_id}
                ,method:"post"
                ,totalRow: false
                ,cols: [[
                    // {type: 'checkbox', fixed: 'left'}
                    {field: 'task_id', title: '任务编号',sort: true,width:180, fixed: 'left'}
                    ,{field: 'task_name', title: '待办任务名称',width:300}
                    ,{field: 'task_des', title: '待办任务描述',width:300}
                    ,{field: 'lssue_time', title: '下发时间' ,width:280}
                    ,{fixed: 'right', title:'操作', toolbar: '#operation',width:190}
                ]]
                ,page:{
                    limits:[5,10,15]
                }
            });

            //监听行工具事件
            table.on('tool(emp_table)', function(obj){
                var data = obj.data;
                if(obj.event === 'emp_del'){
                    $.ajax({
                        url:"/needDealtUpdate.do",
                        type:"post",
                        data:{user_account:user[0].user_account,role_id:user[0].role_id,task_id:data.task_id},
                        success:function (res) {
                            if (res.code == 200){
                                layer.msg(res.msg,{icon:1});
                                table.reload('emp_table', {
                                    where: {
                                        key: {
                                            time: new Date()
                                        }
                                    }
                                }, 'data');
                            }
                           else if (res.code == 500) {
                                layer.msg(res.msg, {icon: 5});
                            }
                            else if (res.code == 502) {
                                layer.msg(res.msg, {icon: 5});
                            }else if (res.code == 405){
                                layer.msg(res.msg,{icon:5});
                            }
                        }
                    });
                } else if(obj.event === 'emp_edit'){
                    console.log(data);
                    var index=layer.open({
                        type: 2,
                        closeBtn: 2,
                        title: ['待办查看', 'font-size:13px;background-color: #23262E;color:white;text-algin:center'],
                        shade: [0.5, '#393D49'],
                        shadeClose:true,
                        area: ['500px', '500px'],
                        content: '../../pages/myWork/viewUpcoming.html',
                        success: function(layero, index){
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            //数据回显
                            body.find('#task_id').text(data.task_id);
                            body.find('#task_name').text(data.task_name);
                            body.find('#line_name').text(data.line_name);
                            body.find('#start_tower').text(data.start_tower);
                            body.find('#stop_tower').text(data.stop_tower);
                            body.find('#lssue_name').text(data.user_name);
                            body.find('#lssue_time').text(data.lssue_time);
                            body.find('#sovle_remarks').text(data.task_des);
                        }
                    });
                }
            });

        } else if (user[0].role_id == 4){
            table.render({
                elem: '#emp_table'
                ,url:'/needDealt.do'
                ,toolbar: '#toolbarDemo'
                ,title: '待办列表'
                ,where:{user_account:user[0].user_account,role_id:user[0].role_id}
                ,method:"post"
                ,totalRow: false
                ,cols: [[
                    // {type: 'checkbox', fixed: 'left'}
                    {field: 'sovle_task', title: '任务编号',sort: true,width:190, fixed: 'left'}
                    ,{field: 'solve_name', title: '待办任务名称',width:165}
                    ,{field: 'sovle_remarks', title: '待办任务描述',width:300}
                    ,{field: 'lssue_time', title: '下发时间',width:240}
                    ,{field: 'user_name', title: '下发人',width:140}
                    ,{fixed: 'right', title:'操作', toolbar: '#operation',width:190}
                ]]
                ,page:{
                    limits:[5,10,15]
                }
            });
            //监听行工具事件
            table.on('tool(emp_table)', function(obj){
                var data = obj.data;
                if(obj.event === 'emp_del'){
                    $.ajax({
                        url:"/needDealtUpdate.do",
                        type:"post",
                        data:{user_account:user[0].user_account,role_id:user[0].role_id,task_id:data.sovle_id},
                        success:function (res) {
                            if (res.code == 200){
                                layer.msg(res.msg,{icon:1});
                                table.reload('emp_table', {
                                    where: {
                                        key: {
                                            time: new Date()
                                        }
                                    }
                                }, 'data');
                            }
                            else if (res.code == 500) {
                                layer.msg(res.msg, {icon: 5});
                            }
                            else if (res.code == 502) {
                                layer.msg(res.msg, {icon: 5});
                            }else if (res.code == 405){
                                layer.msg(res.msg,{icon:5});
                            }
                        }
                    });
                } else if(obj.event === 'emp_edit'){
                    console.log(data);
                    var index=layer.open({
                        type: 2,
                        closeBtn: 2,
                        title: ['待办查看', 'font-size:13px;background-color: #23262E;color:white;text-algin:center'],
                        shade: [0.5, '#393D49'],
                        shadeClose:true,
                        area: ['500px', '500px'],
                        content: '../../pages/myWork/viewUpcoming.html',
                        success: function(layero, index){
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            //数据回显
                            body.find('#task_id').text(data.sovle_task);
                            body.find('#task_name').text(data.solve_name);
                            body.find('#line_name').text(data.line_name);
                            body.find('#start_tower').text(data.start_tower);
                            body.find('#stop_tower').text(data.stop_tower);
                            body.find('#lssue_name').text(data.user_name);
                            body.find('#lssue_time').text(data.lssue_time);
                            body.find('#sovle_remarks').text(data.sovle_remarks);
                        }
                    });
                }
            });
        }else {
            table.render({
                elem: '#emp_table'
                , url: '/defectGrade.do'
                , toolbar: '#toolbarDemo'
                ,title:'缺陷等级'
                , totalRow: false
                , cols: [[
                    {field: 'task_id', title: '任务编号', fixed: 'left',background:'gray'}
                    , {field: 'line_id', title: '线路编号' }
                    , {field: 'tower_id', title: '杆塔编号' }
                    , {field: 'defect_name', title: '缺陷类型',}
                    , {field: 'intact', title: '完好率' }
                    , {field: 'defect_des', title: '缺陷描述'}
                    , {field: 'find_time', title: '发现时间', templet : "<div>{{layui.util.toDateString(d.find_time, 'yyyy-MM-dd')}}</div>"}
                    , {field: 'user_name', title: '发现人员'}
                    , {fixed: 'right', title: '操作', toolbar: '#operation1',width: 80,align:'center',templet:function(row){
                        }}
                ]]
                , page: {
                    limits: [5, 10, 15, 20]
                }
            });
            table.on('tool(emp_table)', function(obj) {
                if (obj.event === 'update_grade') {
                    var data = obj.data;
                    console.log(data);
                    var index=layer.open({
                        type: 2,
                        title: ['确认缺陷级别', 'font-size:13px;background-color: #269ce9;color:white;text-algin:center'],
                        shade: [0.7, 'rgba(37,31,36,0.79)'],
                        area: ['63%', '60%'],
                        content: 'pages/sure_grade.html',
                        success:function(layero,index){
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                            body.find("#task_id").val(data.task_id);
                            body.find("#line_id").val(data.line_id);
                            body.find("#tower_id").val(data.tower_id);
                            body.find("#defect_name").val(data.defect_name);
                            body.find("#intact").val(data.intact);
                            body.find("#defect_des").val(data.defect_des);
                            body.find("#find_time").val(data.find_time);
                            body.find("#user_name").val(data.user_name);
                            body.find("#defect_grade_name").val(data.defect_grade_name);
                        },end:function () {
                            //执行重载
                            table.reload('emp_table', {
                                where: {
                                    key: {
                                        time: new Date()
                                    }
                                }
                            }, 'data');
                            layer.close(index);
                        }
                    });
                }
            });
        }
    });
</script>

</body>
</html>